package top.aigoo.project01.interceptor.shop;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import top.aigoo.project01.entity.Shop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 店家管理系统操作拦截验证器
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/9/21 14:05
 */
public class ShopPermissionInterceptor extends HandlerInterceptorAdapter {
    /*主要做事前拦截，既用户操作发生前，改写preHandle里的逻辑，进行用户操作权限拦截*/
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //从session中获取当前选择的店铺
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        @SuppressWarnings("unchecked")
        //        从session中获取当前用户可操作的店铺列表
        List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
        //非空判断
        if (currentShop != null && shopList != null) {
            //遍历可操作性的店铺列表
            for (Shop shop : shopList) {
                //如果当前店铺在可操作性的列表就返回true，进行接下来的用户操作
                if (shop.getShopId() == currentShop.getShopId()) {
                    return true;
                }
            }
        }
        //若不满足拦截器的验证则返回false，终止用户操作的执行
        return false;
    }
}
